import os.path
from PyQt5.uic import compileUi, compileUiDir, loadUi

import logging

logging.basicConfig(level=logging.INFO,  # filename="test.log",
                    format="[%(asctime)s] %(levelname)s --> %(message)s")
LOG = logging.getLogger(__file__)

# DEFUALT_SUFFIX = "_pag"

def compile_file(path, force_rebuild=True, suffix=""):
    if not os.path.exists(path):
        raise FileNotFoundError

    file_name, _ = os.path.splitext(path)
    compiler_creation = file_name + suffix + ".py"

    if not force_rebuild and os.path.exists(compiler_creation):
        LOG.exception("已存在ui编译文件【{}】，跳过该文件编译".format(path))
        return

    with open(compiler_creation, "w") as fp:
        compileUi(path, fp)
        LOG.info("完成ui文件编译【{}】".format(path))

def compile_dir(path, recurse=True, force_rebuild=True, suffix=""):
    if not os.path.exists(path):
        raise FileNotFoundError

    if force_rebuild:
        compileUiDir(path, recurse)
        LOG.info("完成目录【{}】的 *.ui 文件编译".format(path))
    else:
        # check the param[recurse]
        pass  # go through the dir


if __name__ == "__main__":
    compile_file("./test/form.ui")
    # compile_dir("test")
